'use strict';
const db = uniCloud.database()
const dbCmd = db.command
const $ = dbCmd.aggregate
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)

	const res = await db.collection("uni-id-users")
		.aggregate()
		.match({
			_id: event._id
		})
		.lookup({
			from: 'orders',
			let: {
				paramedicId: '$_id'
			},
			pipeline: $.pipeline()
				.match(dbCmd.expr($.and([
					$.eq(['$paramedic_id', '$$paramedicId']),
					$.eq(['$status', 0])
				])))
				.project({
					_id: 0,
				})
				.done(),
			as: 'orders'
		})
		.lookup({
			from: 'services',
			localField: 'paramedic_profile.service_id',
			foreignField: '_id',
			as: 'service'
		})
		.project({
			gender: 1,
			paramedic_profile: 1,
			orders: 1,
			service: 1
		})
		.end()

	console.log(res)

	//返回数据给客户端
	return res
};
